package mod

import (
	"github.com/simonvetter/modbus"
	"time"
)

var Am *AranMod

type AranMod struct {
	Client *modbus.ModbusClient
}

func New(ip, port string) (*AranMod, error) {
	client, err := modbus.NewClient(&modbus.ClientConfiguration{
		URL:     "tcp://" + ip + ":" + port,
		Timeout: 1 * time.Second,
	})
	if err != nil {
		return nil, err
	}
	return &AranMod{
		Client: client,
	}, nil
}
func (a *AranMod) ReadDI() ([]bool, error) {
	return a.Client.ReadDiscreteInputs(0x10, 6)
}

func (a *AranMod) ReadDO() ([]bool, error) {
	return a.Client.ReadCoils(0x10, 6)
}

func (a *AranMod) WriteDO(i uint16, b bool) error {
	return a.Client.WriteCoil(i, b)
}
